<?php
/**
 * Created by PhpStorm.
 * User: Ss
 * Date: 2018/6/5
 * Time: 下午4:36
 */
namespace common\libs;
class BlockChain
{
    private $callback_wallet = '';

    private $callback_status = '';

    private $bot = '';

    private $private_key = '';

    private $request_url = '';

    private $rpc_url = '';

    // 表单提交字符集编码
    public $postCharset = "UTF-8";

    private $fileCharset = "UTF-8";

    public function setPrivateKey($privateKey)
    {
        $this->private_key = $privateKey;
    }

    public function getPrivateKey()
    {
        return $this->private_key;
    }

    public function setBot($bot)
    {
        $this->bot = $bot;
    }

    public function getBot()
    {
        return $this->bot;
    }

    public function setRequestUrl($requestUrl)
    {
        $this->request_url = $requestUrl;
    }

    public function getRequestUrl()
    {
        return $this->request_url;
    }

    public function setCallBackWallet($callBackWallet)
    {
        $this->callback_wallet = $callBackWallet;
    }

    public function getCallBackWallet()
    {
        return $this->callback_wallet;
    }

    public function setCallBackStatus($callBackStatus)
    {
        $this->callback_status = $callBackStatus;
    }

    public function getCallBackStatus()
    {
        return $this->callback_status;
    }

    public function setRpcUrl($rpcUrl)
    {
        $this->rpc_url = $rpcUrl;
    }

    public function getRpcUrl()
    {
        return $this->rpc_url;
    }

    public function getWalletUrl($msg)
    {
        return $this->request_url.'bot='.$this->bot.'&msg='.$msg;
    }

    public function getSign($params)
    {
        $params = $this->getSignContent($params);
        $params .= $this->private_key;
        return md5($params);
    }

    public function getSignContent($params) {
        ksort($params);
        $stringToBeSigned = "";
        $i = 0;
        foreach ($params as $k => $v) {
//            if (false === $this->checkEmpty($v) && "@" != substr($v, 0, 1)) {
             if ("@" != substr($v, 0, 1)) {
                // 转换成目标字符集
                $v = $this->characet($v, $this->postCharset);

                if ($i == 0) {
                    $stringToBeSigned .= "$k" . "=" . "$v";
                } else {
                    $stringToBeSigned .= "&" . "$k" . "=" . "$v";
                }
                $i++;
            }
        }
        unset ($k, $v);
        return $stringToBeSigned;
    }

    /**
     * 转换字符集编码
     * @param $data
     * @param $targetCharset
     * @return string
     */
    function characet($data, $targetCharset) {

        if (!empty($data)) {
            $fileType = $this->fileCharset;
            if (strcasecmp($fileType, $targetCharset) != 0) {
                $data = mb_convert_encoding($data, $targetCharset, $fileType);
                //				$data = iconv($fileType, $targetCharset.'//IGNORE', $data);
            }
        }
        return $data;
    }

    /**
     * 校验$value是否非空
     *  if not set ,return true;
     *    if is null , return true;
     **/
    protected function checkEmpty($value) {
        if (!isset($value))
            return true;
        if ($value === null)
            return true;
        if (trim($value) === "")
            return true;

        return false;
    }
}